package io.github.wanggit.antrpc.commons.codec.serialize.json;

import io.github.wanggit.antrpc.commons.codec.serialize.ISerializer;
import io.github.wanggit.antrpc.commons.codec.serialize.SerializeRuntimeException;
import io.github.wanggit.antrpc.commons.utils.JsonUtils;

import java.util.Map;

public class JsonSerializer implements ISerializer {

    @Override
    public void setConfigs(Map<String, String> configs) {}

    @Override
    public void init() {}

    @Override
    public byte[] serialize(Object object) {
        try {
            return JsonUtils.toJsonBytes(object);
        } catch (Exception e) {
            throw new SerializeRuntimeException(e);
        }
    }

    @Override
    public <T> T deserialize(byte[] buf, Class<T> tClass) {
        try {
            return JsonUtils.fromJson(buf, tClass);
        } catch (Exception e) {
            throw new SerializeRuntimeException(e);
        }
    }
}
